package com.ml.springboot.controller;

import com.ml.springboot.pojo.Employee;
import com.ml.springboot.pojo.Leave;
import com.ml.springboot.pojo.query.LeaveQuery;
import com.ml.springboot.service.ILeaveService;
import com.ml.springboot.util.PageResult;
import com.ml.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/leave")
public class LeaveController {
/*    @Autowired
    private LeaveMapper LeaveMapper;*/
    @Autowired
    private ILeaveService leaveService;
    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(LeaveQuery leaveQuery) {
        System.out.println("LeaveController.list");
        PageResult pageResult = leaveService.list(leaveQuery);
        return pageResult;
    }

    @RequestMapping("/toLeaveList")
    public String toLeaveList() {
        return "leave_list";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deletedById(Integer id) {
        leaveService.deletedById(id);
        return Result.ok("删除成功");
    }
    @RequestMapping("/pass")
    @ResponseBody
    public Result pass_leave(Integer id) {
        leaveService.pass(id);
        return Result.ok("已通过改审核！");
    }
    @RequestMapping("/nopass")
    @ResponseBody
    public Result nopass_leave(Integer id) {
        leaveService.nopass(id);
        return Result.ok("已拒绝改审核！");
    }
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deletedAll(Integer[] ids) {
        leaveService.deleteAll(ids);
        return Result.ok("删除成功");
    }
    @RequestMapping("/toLeaveAdd")
    public String toLeaveAdd() {
        return "Leave_add";
    }
    @RequestMapping("/toLeaveAudit")
    public String toLeaveAudit() {
        return "leave_audit_list";
    }
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Leave leave, HttpSession session) {
        Employee employee = (Employee) session.getAttribute("employee");
        leave.setEmployeeId(employee.getId());
        leaveService.add(leave);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toLeaveUpdate")
    public String toLeaveUpdate(Leave Leave) {
        return "leave_update";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Leave leave = leaveService.selectById(id);
        return Result.ok(leave);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Leave leave) {
        leaveService.update(leave);
        return Result.ok("修改成功");
    }
}
